package com.chengdu.third.controller;

import com.chengdu.third.dao.ILawsDao;
import com.chengdu.third.pojo.Laws;
import com.chengdu.third.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @author jingbo
 * @create 2021--08--20  17:35
 */

@Controller
public class denglu {


    @Autowired
    private ILawsDao iLawsDao;

    @Autowired
    private IUserService iUserService;

    @GetMapping("/loginpage")
    public String logiona(){
        return "login";
    }
    @GetMapping("/registerpage")
    public String signup(){
        return "register";
    }




    @PostMapping("/loginqiantai")
    public String logintest(@RequestParam String username, @RequestParam String password,
                            RedirectAttributes attributes,@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum,
                            Model model){
        int login1 = iUserService.login(username, password);
        if(login1!=1){
            attributes.addFlashAttribute("message", "用户名或密码错误");
            return "redirect:/loginpage";
        }else {
            PageHelper.startPage(pagenum, 5);
            List<Laws> selectshixian = iLawsDao.selectshixian();

            List<Laws> selectxiangzheng = iLawsDao.selectxiangzheng();
            List<Laws> selectcun = iLawsDao.selectcun();

            PageInfo<Laws> pageInfo = new PageInfo<>(selectshixian);
            PageInfo<Laws> xiang = new PageInfo<>(selectxiangzheng);
            PageInfo<Laws> cun = new PageInfo<>(selectcun);

            model.addAttribute("pageInfo", pageInfo);
            model.addAttribute("xiang", xiang);
            model.addAttribute("cun", cun);
            return "index1";
        }

    }

    @PostMapping("/zhuce")
    public String signup(@RequestParam String username,@RequestParam String password,
                         @RequestParam String phone, RedirectAttributes attributes,@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum,
                         Model model){
       iUserService.addUser1(username,password,phone);
        PageHelper.startPage(pagenum, 5);
        List<Laws> selectshixian = iLawsDao.selectshixian();

        List<Laws> selectxiangzheng = iLawsDao.selectxiangzheng();
        List<Laws> selectcun = iLawsDao.selectcun();

        PageInfo<Laws> pageInfo = new PageInfo<>(selectshixian);
        PageInfo<Laws> xiang = new PageInfo<>(selectxiangzheng);
        PageInfo<Laws> cun = new PageInfo<>(selectcun);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("xiang", xiang);
        model.addAttribute("cun", cun);
        return "index1";
    }
}
